package com.sora.product.service.impl;

import com.sora.basic.service.IBaiduAiAuditService;
import com.sora.basic.util.AjaxResult;
import com.sora.basic.util.BaiduAiUtils;
import com.sora.product.domain.Product;
import com.sora.product.domain.ProductOnlineAuditLog;
import com.sora.product.domain.Product;
import com.sora.product.domain.ProductDetail;
import com.sora.product.mapper.ProductDetailMapper;
import com.sora.product.mapper.ProductMapper;
import com.sora.product.mapper.ProductOnlineAuditLogMapper;
import com.sora.product.service.IProductService;
import com.sora.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Override
    public void save(Product product) {
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void del(Long id) {
        productDetailMapper.delByProductId(id);
        super.del(id);
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.updateByProductId(productDetail);
        }
    }

    @Override
    public void batchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            //获取需要审核的信息
            String productName = product.getName();
            String resources = product.getResources();
            Boolean boo1 = baiduAiAuditService.textAudit(productName);
            Boolean boo2 = baiduAiAuditService.imageAudit(resources);

            if(boo1 && boo2){ //两个审核结果都为true，表示审核通过
                //添加审核日志，更改宠物状态,和上架时间
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLog.setNote(id+"审核成功！");
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                productMapper.updateOnsaleState(ids,new Date());
            }else{
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setAuditTime(new Date());
                productOnlineAuditLog.setNote(id+"审核失败！");
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                return new AjaxResult(false,"上架失败！");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids) {
        //更改宠物状态，添加下架时间
        productMapper.updateOffsaleState(ids,new Date());
    }
}
